﻿#include "dialog.h"
#include "ui_dialog.h"
#include <QDebug>
#include <QMessageBox>
#include <QFileDialog>
#define _USE_MATH_DEFINES
#include "math.h"
#include <QRegExpValidator>
#include <QTextCodec>

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);
    QRegExp limit("(^(0{1,}([1-5]\\d){0,1})|(^(\\d|[1-5]\\d){0,1})|^(0{1,}(\\d){1}))+(\\.|\\.\\d+)?$");
    QRegExpValidator *plimit = new QRegExpValidator(limit);
    ui->lineEdit_2->setValidator(plimit);
    QStringList strlist;
    ui->comboBox->clear();
    strlist<<"5.5"<<"8.0"<<"9.0";
    ui->comboBox->addItems(strlist);
    ui->comboBox->clearEditText();

    //MyTableWidget->setCurrentCell(2,3);
     ui->tableWidget->setRowCount(10);
     ui->tableWidget->setColumnCount(3);
\
    QStringList header,header0;
    header0<<"第一列"<<"第二列"<<"第三列";
    ui->tableWidget->setHorizontalHeaderLabels(header0);

    ui->tableWidget->setItem(0,0,new QTableWidgetItem("Jan0"));
    ui->tableWidget->setItem(0,1,new QTableWidgetItem("Jan1"));
    ui->tableWidget->setItem(0,2,new QTableWidgetItem("Jan2"));

    ui->tableWidget->setItem(1,0,new QTableWidgetItem("Tom0"));
    ui->tableWidget->setItem(1,1,new QTableWidgetItem("Tom1"));
    ui->tableWidget->setItem(1,2,new QTableWidgetItem("Tom2"));

    //ui->tableWidget->setItem(2,0,new QTableWidgetItem("Ana0"));
    //ui->tableWidget->setItem(2,1,new QTableWidgetItem("Ana1"));
    QTableWidgetItem *testitem=new QTableWidgetItem("Ana2");
    ui->tableWidget->setItem(2,2,testitem);
    //ui->tableWidget->setCurrentCell(0,QItemSelectionModel::Select);
    //delete testitem;
    header<<"1"<<"2"<<"3";
    QModelIndexList indexs=ui->tableWidget->selectionModel()->selectedRows();

    ui->tableWidget->setItem(1,0,new QTableWidgetItem("LiM0"));
    ui->tableWidget->setItem(1,1,new QTableWidgetItem("LiM1"));
    ui->tableWidget->setItem(1,2,new QTableWidgetItem("LiM2"));

}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::on_loadfromtxt_clicked()
{
    QFile file;
    //QString f=QFileDialog::getOpenFileName(this,"选择文件","","文本文件(*.txt);;All(*.*)");
    QString f="E:\\ceshi\\1.dingwei\\double\\1 iamges（飞行方向朝上）\\01.txt";
    file.setFileName(f);
    if(file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        int num=0;
        QTextCodec *codec=QTextCodec::codecForName("GBK");
        while (!file.atEnd())
        {
            QByteArray line=file.readLine();
            QString txtstr=codec->toUnicode(line);
            num+=1;
            //analysis_args(txtstr);
            QStringList numlist=txtstr.split(QRegExp("[^\\d^\\.\\+\\-]+"),QString::SkipEmptyParts);
            str=(txtstr.split(QRegExp(":|\\(|\\)|："),QString::SkipEmptyParts)).at(0);

            for (int i=0;i<numlist.size();i++)
            {
                str=str+"？"+numlist[i];
            }

            ui->textEdit->append(QString::number(num)+":"+str);
        }
        file.close();
    }
}


void Dialog::on_savetotxt_clicked()
{
    QString e = "ui->text_e->toPlainText()\n";
    QFile file;
    file.setFileName(QFileDialog::getSaveFileName(this,"保存路径","/","文本文档(*.txt)"));
    file.open(QIODevice::WriteOnly | QIODevice::Text);
     file.write(e.toUtf8());
    e="4515145";
    file.write(e.toUtf8());
    file.close();

}


void Dialog::on_getpath_clicked()
{
    double e = 0.081819190842621;
    qDebug()<<1/(1-sqrt(1-e*e));
    ui->lineEdit->setText(QCoreApplication::applicationDirPath());
}


void Dialog::on_show_clicked()
{
    on_loadfromtxt_clicked();
    QString strlist=(str.split(QRegExp(":|\\(|\\)"),QString::SkipEmptyParts)).at(0);
    double num=ui->comboBox->currentText().toDouble();
    QString num_str=QString::number(num,'g',10);
    ui->lineEdit->setText(num_str);
    qDebug()<<simplify_dnum(".0000000");
}


void Dialog::on_checkBox_stateChanged(int arg1)
{
    QString str=QString::number(arg1);
    ui->textEdit->append(str);
}

void Dialog::on_deletebutton_clicked()
{
    ui->tableWidget->selectAll();
    //ui->tableWidget->removeRow(0);
}

int Dialog::simplify_dnum(QString rawstr)
{
    QString dotstr;
    int dot=rawstr.indexOf(".");
    if (-1==dot)
        return 0;
    else
        dotstr=rawstr.mid(dot+1);

    if (0==dotstr.toDouble())
        return 0;
    for (int i=dotstr.size()-1;i>-1;i--)
    {
        if (i==dotstr.lastIndexOf("0"))
        {
            dotstr=dotstr.mid(0,i);
            continue;
        }
        else
            return i+1;
    }
}
